<?

/**
 *  Copyright 2008 Ivan Grosny 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at 
 *  http://www.apache.org/licenses/LICENSE-2.0 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  @author Ivan Grosny
 */

require_once 'flash.php';

class Db
{
	static $link;
	
	static function connect()
	{
		self::$link = @mysql_connect($_SESSION['db_host'].':'.$_SESSION['db_port'],	$_SESSION['db_user'],$_SESSION['db_pass']);
		if(!self::$link)
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return 1;
		}
	}
	
	static function query($query)
	{
		$result = mysql_query(stripslashes($query), self::$link);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}
	
	static function select_db($db)
	{
		$result = mysql_select_db($db, self::$link);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}
	
	static function create_schema($db)
	{
		$result = mysql_query("create database ".$db, self::$link);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}
		
		
	static function list_dbs()
	{
		$result = mysql_list_dbs(self::$link);

		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			$array = array();
			if(trim($_SESSION['db_name']) != "")
			{
				$array[] = array($_SESSION['db_name']);
				return $array;
			}
		
			while($row = mysql_fetch_row($result))
			{
				$array[] = $row;
			}
			return $array;
		}
	}	
	
	static function list_tables($db)
	{
		$result = mysql_list_tables($db,self::$link);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}	
	
	static function fetch_object($result)
	{
		$result = mysql_fetch_object($result);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}
	
	static function fetch_row($result)
	{
		$result = mysql_fetch_row($result);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}	
	
	static function num_fields($result)
	{
		$result = mysql_num_fields($result);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}

	static function fetch_field($result)
	{
		$result = mysql_fetch_field($result);
		if(mysql_error())
		{
			Flash::ERROR(mysql_error());
			return 0;
		}
		else
		{
			return $result;
		}
	}								
}
?>